from contract import *
import json

bootstrap()
collected = {
    '_env': 'test',
}


def collect(name, data):
    collected[name] = {
        'data': data,
    }

# 节点信息校验


def validate_node(node):
    assert 'id' in node
    assert 'host' in node or 'ip' in node
    assert 'p2p_port' in node


# 遍历网络，限制遍历50次
found_nodes = {}


def find_nodes(node, iter_count=0):
    iter_count += 1
    if (iter_count > 50):
        exit
    found_nodes[node['host']] = node
    peers = get_node_peers(node)
    for peer in peers['peers']:
        if peer['host'] not in found_nodes:
            found_nodes[peer['host']] = peer
        else:
            find_nodes(peer)

# --- 测试流程

# --- 1. 身份管理


# 获取样例组织信息
org_info = get_config_org_info()
assert type(org_info) is dict
assert 'name' in org_info
collect('get_config_org_info', org_info)

# 组织加入联盟
org = add_org(org_info)
assert type(org) is dict
assert 'id' in org
assert 'name' in org
collect('add_org', org)

# 获取样例用户信息
user_info = get_config_user_info(org)
assert type(user_info) is dict
assert 'org_id' in user_info
collect('user_info', user_info)

# 用户加入组织
user = add_user(user_info, org)
assert type(user) is dict
assert 'id' in user
assert 'org_id' in user
collect('add_user', user)

# --- 2. 网络管理

# 获取样例节点配置
node_info = get_config_node_info()
assert type(node_info) is dict
assert 'id' in node_info
collect('get_config_node_info', node_info)

# # 添加节点
# node = add_node(node_info, org)
# assert type(node) is dict
# validate_node(node)
# collect('add_node', node)

# # 删除节点
# remove_node(node, org)

# 获取节点邻居
node = {'host': 'node0'}
peers = get_node_peers(node)
assert type(peers) is dict
assert 'node' in peers
assert type(peers['node']) is dict
assert 'peers' in peers
assert type(peers['peers']) is list
assert len(peers['peers']) > 0
for peer in peers['peers']:
    validate_node(peer)
collect('get_node_peers', peers)

# find_nodes(node)
# collect('find_nodes', found_nodes)

# 获取参与组网的全部节点信息
nodes = get_network_nodes()
assert type(nodes) is list
for node in nodes:
    validate_node(node)
collect('get_network_nodes', nodes)

# --- 3. 账本统计

# 当前区块高度
current_block_height = get_current_block_height()
assert type(current_block_height) is int
collect('get_current_block_height', current_block_height)

# 根据区块高度获取区块信息
block = get_block(height=current_block_height)
assert type(block) is dict
assert 'height' in block
assert 'hash' in block
assert 'timestamp' in block
assert 'full_json' in block
assert 'tx_count' in block
collect('get_block', block)

# 根据区块高度获取交易列表
tx_list = get_tx_by_block(height=current_block_height)
assert type(tx_list) is list
assert len(tx_list) > 0
for tx in tx_list:
    assert 'txid' in tx
collect('get_tx_by_block', tx_list)

# 根据交易ID获取交易列表
tx1 = get_tx_by_id(tx_list[0]['txid'])
assert type(tx1) is dict
assert 'txid' in tx1
assert 'timestamp' in tx1
assert 'full_json' in tx1
collect('get_tx_by_id', tx1)

# --- 4. 交易处理

tx2 = create_tx()
collect('create_tx', tx2)

# 发送交易
tx2 = send_tx(tx2)
assert type(tx2) is dict
assert 'txid' in tx2
collect('send_tx', tx2)

# 部署合约
tx3 = deploy_contract()
assert type(tx3) is dict
assert 'txid' in tx3
collect('deploy_contract', get_tx_by_id(tx3['txid']))

with open('collected-test.json', 'w') as f:
    json.dump(collected, f, indent=2)
